﻿using System.Collections.Generic;
using System;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;

namespace Marbles.Core.Web.MVC.Models
{
    public abstract class Model : IModel
    {
        public Enclosures.Enclosure Enclosure
        {
            get { return this._Enclosure; }
        }

        public Views.View View
        {
            get { return this._View; }
        }

        private Views.View _View = null;
        private Enclosures.Enclosure _Enclosure = null;

        public abstract void Load(object sender, EventArgs e);
        public abstract void UnLoad(object sender, EventArgs e);

        public object Parameters(string Name)
        {
            string value = HttpContext.Current.Request.Params[Name];

            if ((value == null) || (value == string.Empty)) { return value; }
            
            if ((value.Trim().ToLower() == "true") || (value.Trim().ToLower() == "false"))
            {
                bool boolvalue = false;
                if (bool.TryParse(value.Trim(), out boolvalue)) { return boolvalue; }
            }

            if (value.Contains("."))
            {
                decimal dvalue = 0;
                if (decimal.TryParse(value.Trim(), out dvalue)) { return dvalue; }
            }

            long ivalue = 0;
            if (long.TryParse(value.Trim(), out ivalue)) { return ivalue; }

            DateTime datevalue = DateTime.MinValue;
            if (DateTime.TryParse(value.Trim(), out datevalue)) { return datevalue; }

            return value;
        }

        internal void InitialiseView(Views.View View)
        {
            this._View = View;
        }
    }
}
